/*
 * Created on 2005-6-28
 * Last modified on 2008-4-18
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.commons.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.log4j.Logger;

/**
 * @deprecated
 * @author aroot
 *
 */
public class FormatDateTime {

	private static Logger logger = Logger.getLogger(FormatDateTime.class);

	public static final int SECOND = 13;

	public static final int DAY_OF_MONTH = 5;

	public static final int DAY_OF_YEAR = 6;

	public static final int DAY_OF_WEEK = 7;

	public static final int HOUR_OF_DAY = 11;

	public static final int WEEK_OF_YEAR = 3;

	public static final int MONTH = 2;

	public static final int YEAR = Calendar.YEAR;

	public static String formatToHtml(String sb) {
		if (ValidatorHelper.isEmpty(sb)) {
			return "";
		} else if (sb.length() == 14) {
			sb = FormatDateTime.decodeTime(sb);
		}
		String d = "";
		String t = "";
		sb = sb.replace(".0", "");
		d = sb.substring(0, sb.indexOf(" ")); /* 取年月 */
		t = sb.substring(sb.indexOf(" ") + 1, sb.length() - 3); /* 取时间去掉秒数 */
		String ts = "";
		boolean isAfter;
		try {
			String now = FormatDateTime.formatDateTime("yyyy-MM-dd");
			isAfter = FormatDateTime.dateCompare(d, now);
			if (!isAfter) { /* 今天 */
				d = d.substring(2, d.length());
				ts = ("<font style=\"font-size:12px; color:#cc0000;font-weight:bold;\">今天</font> ");
				ts += (" <font style=\"font-size:12px; color:#990066\">" + t);
				ts += ("</font>");
			} else {
				/* 计算昨天 */
				ts = FormatDateTime.dateAdd(FormatDateTime.DAY_OF_MONTH, -1); /* 返回间隔一天后的时候 */
				ts = ts.substring(0, 11); /* 取年月日 */
				isAfter = FormatDateTime.dateCompare(d, ts);
				if (!isAfter) {
					d = d.substring(2, d.length());
					ts = ("<font style=\"font-size:12px; color:#006600;font-weight: bold;\">昨天</font> ");
					ts += (" <font style=\"font-size:12px; color:#990066\">" + t);
					ts += ("</font>");
				} else {
					/* 计算前天 */
					ts = FormatDateTime.dateAdd(FormatDateTime.DAY_OF_MONTH, -2); /* 返回间隔两天后的时候 */
					ts = ts.substring(0, 11); /* 取年月日 */
					isAfter = FormatDateTime.dateCompare(d, ts);
					if (!isAfter) {
						d = d.substring(2, d.length());
						ts = ("<font style=\"font-size:12px; color:#2A00FF;font-weight: bold;\">前天</font> ");
						ts += (" <font style=\"font-size:12px; color:#990066\">" + t);
						ts += ("</font>");
					} else {
						d = d.substring(2, d.length());
						ts = (d + " <font style=\"color:#990066\">" + t);
						ts += ("</font>");
					}
				}
			}
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return ts;
	}

	/**
	 * 设置间隔数后返回时间
	 * 
	 * @param type
	 *            间隔数类型 s秒 d天 h小时
	 * @param i
	 *            间隔数
	 * @param fromat_str
	 *            格式化时间的方式 如yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String dateAdd(int type, int i, String fromat_str) {
		return dateAdd(FormatDateTime.formatDateTime(fromat_str), type, i, fromat_str);
	}

	public static long now() {
		return TypeChange.stringToLong(formatDateTime("yyyyMMddHHmmss"));
	}

	public static long formatDateTimeToLong(String ymd) {
		return TypeChange.stringToLong(formatDateTime(ymd));
	}

	public static int formatDateTimeToInt(String ymd) {
		return TypeChange.stringToInt(formatDateTime(ymd));
	}

	/**
	 * 日期类操作工具
	 */
	public static String formatDateTime(String ymd) {
		// 格式化当前时间
		java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat(ymd);
		return isNow.format(new java.util.Date());
	}

	public static String formatDateTime(String ymd, String datetime) {
		// 格式化当前时间
		java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat(ymd);
		String now = "";
		try {
			now = isNow.format(datetime);
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return now;
	}

	public static String decodeTime(long time) {
		return decodeTime(String.valueOf(time));
	}

	public static String decodeTime(String time) {
		StringBuffer sb = new StringBuffer("");
		String str = time;
		if (str.length() >= 14) {
			sb.append(str.substring(0, 4));
			sb.append("-");
			sb.append(str.substring(4, 6));
			sb.append("-");
			sb.append(str.substring(6, 8));
			sb.append(" ");
			sb.append(str.substring(8, 10));
			sb.append(":");
			sb.append(str.substring(10, 12));
			sb.append(":");
			sb.append(str.substring(12, 14));
		}
		return sb.toString();
	}

	public static boolean dateCompare(long time) {
		if (time > 0) {
			return dateCompare(decodeTime(time));
		} else {
			return false;
		}
	}

	/**
	 * 比较当前日期和指定日期 return boolean 如果当前日期在指定日期之后返回true否则返回flase
	 * 
	 * @param str
	 * @return
	 */
	public static boolean dateCompare(String str) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
		String isDate = sdf_d.format(new java.util.Date());
		java.util.Date date1;
		java.util.Date date0;
		try {
			date1 = sdf_d.parse(str);
			date0 = sdf_d.parse(isDate);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException e) {
			logger.error(e.toString());
		}
		return bea;
	}

	/**
	 * 比较当前月份和指定月份 如果当前月份在指定月份之后返回true否则返回flase
	 * 
	 * @param str
	 * @return
	 */
	public static boolean monthCompare(String str) {
		boolean bea = false;
		SimpleDateFormat sdf_m = new SimpleDateFormat("yyyy-MM");
		String isMonth = sdf_m.format(new java.util.Date());
		java.util.Date date1;
		java.util.Date date0;
		try {
			date1 = sdf_m.parse(str);
			date0 = sdf_m.parse(isMonth);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException e) {
			logger.error(e.toString());
		}
		return bea;
	}

	/**
	 * 比较当前日期和指定日期 return boolean 如果当前日期在指定日期之后返回true否则返回flase
	 * 
	 * @param str
	 * @return
	 */
	public static boolean secondCompare(String str) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String isDate = sdf_d.format(new java.util.Date());
		java.util.Date date1;
		java.util.Date date0;
		try {
			date1 = sdf_d.parse(str);
			date0 = sdf_d.parse(isDate);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException e) {
			logger.error(e.toString());
		}
		return bea;
	}

	/**
	 * 比较指定两日期如果str1晚于str2则return false; 精确到秒
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean secondCompare(String str1, String str2) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date date1;
		java.util.Date date0;
		try {
			date1 = sdf_d.parse(str1);
			date0 = sdf_d.parse(str2);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException e) {
			logger.error(e.toString());
		}
		return bea;
	}

	/**
	 * 比较指定两日期如果str1晚于str2则return flase; 精确到天
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean dateCompare(String str1, String str2) {
		boolean bea = false;
		SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date1;
		java.util.Date date0;
		try {
			date1 = sdf_d.parse(str1);
			date0 = sdf_d.parse(str2);
			if (date0.after(date1)) {
				bea = true;
			}
		} catch (ParseException e) {
			logger.error(e.toString());
		}
		return bea;
	}

	/**
	 * 设置间隔数后返回时间
	 * 
	 * @param type
	 * @param i
	 * @return
	 */
	public static String dateAdd(int type, int i) {
		return dateAdd(type, i, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 给指定的时间按间隔数设置后返回间隔时间
	 * 
	 * @param str
	 *            格式化过的时间，要和fromat_str格式化方式一致
	 * @param type
	 *            间隔数类型 s秒 d天 h小时
	 * @param i
	 *            间隔数
	 * @param fromat_str
	 *            格式化时间的方式 如yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String dateAdd(String time, int type, int i, String fromat_str) {
		SimpleDateFormat df = new SimpleDateFormat(fromat_str);
		Calendar c = Calendar.getInstance(); // 当时的日期和时间
		c.set(type, c.get(type) + i);
		return df.format(c.getTime());
	}

	public static void main(String args[]) {
		long l = FormatDateTime.now();
		System.out.println(l);
		long s = TypeChange.stringToLong(FormatDateTime.dateAdd(FormatDateTime.SECOND, 300, "yyyyMMddHHmmss"));
		System.out.println(s);
	}

}
